/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2021-01-08
*/
package com.rzico.article.service;

import com.rzico.annotation.Log;
import com.rzico.article.entity.Article;
import com.rzico.article.entity.Talent;
import com.rzico.base.BaseMapper;
import com.rzico.base.CommResult;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.article.entity.ArticleLaud;
import com.rzico.basics.entity.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.stereotype.Service;
import com.rzico.article.mapper.ArticleLaudMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 笔记点赞业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class ArticleLaudService extends BaseServiceImpl<ArticleLaud, String> {

    @Autowired
    private ArticleLaudMapper articleLaudMapper;

    @Autowired
    @Lazy
    private ArticleService articleService;

    @Autowired
    private TalentService talentService;


    @Override
    public BaseMapper<ArticleLaud, String> getMapper() {
        return articleLaudMapper;
    }

    /**
     * 查询文章详情的时候查自己是否点赞
     * @return
     */
    public  boolean getIsAlaud(Map<String,Object> params) {
        List<ArticleLaud> articleLauds = selectList(params);
        if (articleLauds != null && articleLauds.size() != 0) {
            return true;
        }
        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    public ArticleLaud saveArticleLaud(Member member, ArticleLaud articleLaud) {
        //查看有没有这篇文章
        Article article = articleService.findById(articleLaud.getArticleId());
        if (article == null) {
            return null;
        }
        int affectCount = 0;
        articleLaud.setMemberId(member.getId());
        Map<String,Object> params = new HashMap<>();
        if (articleLaud.getArticleId() != null) {
            params.put("articleId", articleLaud.getArticleId());
            params.put("memberId", articleLaud.getMemberId());
        }
        List<ArticleLaud> articleLauds = selectList(params);
        if (articleLauds != null && articleLauds.size() != 0) {
            articleLaud = articleLauds.get(0);
            affectCount = updateByPrimaryKey(articleLaud);
        } else {
            articleLaud.setMemberId(member.getId());
            articleLaud.setNickname(member.getUsername());
            articleLaud.setAvatar(member.getAvatar());
            affectCount = insert(articleLaud);
        }
        if (affectCount <= 0) {
            return null;
        } else {
            //点赞数加1
            if (article.getLaudNum() == null) {
                article.setLaudNum(0L);
            }
            article.setLaudNum(article.getLaudNum()+1);
            articleService.updateByPrimaryKey(article);
        }
        ArticleLaud result = findById(articleLaud.getId());

        //点赞数+1
        Talent talent = talentService.findById(article.getTalentId());
        if (talent != null) {
            if (talent.getLaud() == null) {
                talent.setLaud(0);
            }
            talent.setLaud(talent.getLaud()+1);
            talentService.updateByPrimaryKey(talent);
        }
        return result;
    }

}
